package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.dto.IdsDTO;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.dto.GoodsCommentDTO;
import cn.signalpha.modules.shop.entity.Goods;
import cn.signalpha.modules.shop.entity.GoodsComment;
import cn.signalpha.modules.shop.service.GoodsCommentService;
import cn.signalpha.modules.shop.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;
import java.util.Objects;


@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/goods/comment")
@Api(value = "商品评论", tags = "商品评论")
public class GoodsCommentController extends BaseController {

	private final GoodsService goodsService;
	private final GoodsCommentService goodsCommentService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="sellerId", dataType = "string", value = "商户id"),
		@ApiImplicitParam(name="isShow", dataType = "string", value = "是否显示"),
		@ApiImplicitParam(name="isReply", dataType = "string", value = "是否回复"),
		@ApiImplicitParam(name="goodsName", dataType = "string", value = "商品名称"),
		@ApiImplicitParam(name="userNickName", dataType = "string", value = "用户名称"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<GoodsComment>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		QueryWrapper<GoodsComment> wrapper = goodsCommentService.selectGoodsCommentWrapper(params);
        return R.data(goodsCommentService.page(Condition.getPage(query), wrapper));
    }

	@PostMapping("/submit")
	@ApiOperation(value = "添加自评", notes = "")
	public R submit(@Valid @RequestBody GoodsCommentDTO goodsCommentDTO) {
		Goods goods = goodsService.getById(goodsCommentDTO.getGoodsId());
		Assert.isNull(goods, "商品不存在");

		GoodsComment goodsComment = Objects.requireNonNull(BeanUtil.copy(goodsCommentDTO, GoodsComment.class));
		goodsComment.setSellerId(goods.getSellerId());
		goodsComment.setGoodsImg(goods.getImg());
		goodsComment.setGoodsName(goods.getName());
		goodsComment.setCreateTime(new Date());
		goodsCommentService.saveOrUpdate(goodsComment);
		if(goodsCommentDTO.getId() == null){
			goodsService.updateById(Goods.builder().id(goods.getId()).commentNum(goods.getCommentNum() + 1).build());
		}
		return R.status(true);
	}

	@PostMapping("/reply")
	@ApiOperation(value = "回复", notes = "")
	public R reply(@Valid @RequestBody GoodsCommentDTO.ReplyDTO goodsCommentDTO) {
		GoodsComment goodsComment = goodsCommentService.getById(goodsCommentDTO.getId());
		Assert.isNull(goodsComment, MsgConstant.OBJECT_NULL);
		return R.status(goodsCommentService.saveOrUpdate(BeanUtil.copy(goodsCommentDTO, GoodsComment.class)));
	}

	@PostMapping("/show")
	@ApiOperation(value = "显示", notes = "传入ids")
	public R show(@Valid @RequestBody IdsDTO idsDTO) {
		goodsCommentService.update(GoodsComment.builder().isShow(1).build(),
			Wrappers.<GoodsComment>lambdaUpdate().in(GoodsComment::getId, Func.toLongList(idsDTO.getIds()))
		);
		return R.status(true);
	}

	@PostMapping("/hide")
	@ApiOperation(value = "隐藏", notes = "传入ids")
	public R hide(@Valid @RequestBody IdsDTO idsDTO) {
		goodsCommentService.update(GoodsComment.builder().isShow(0).build(),
			Wrappers.<GoodsComment>lambdaUpdate().in(GoodsComment::getId, Func.toLongList(idsDTO.getIds()))
		);
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(goodsCommentService.removeByIds(Func.toLongList(ids)));
	}

}
